package com.hzit.sk.service.impl;

import com.hzit.sk.constant.RedisKeyConstant;
import com.hzit.sk.mapper.GoodsMapper;
import com.hzit.sk.mapper.SkillGoodsMapper;
import com.hzit.sk.model.GoodsDeatailInfo;
import com.hzit.sk.model.SkillGoods;
import com.hzit.sk.model.SkillGoodsInfo;
import com.hzit.sk.resp.GoodsDeatailData;
import com.hzit.sk.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/***
 * 商品相关业务接口实现类
 */
@Service
public class GoodsServiceImpl implements IGoodsService, InitializingBean {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ValueOperations valueOperations;

    @Autowired
    private SkillGoodsMapper skillGoodsMapper;

//    @Autowired
//    private ListOperations listOperations;
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);


    @Override
    public  List<SkillGoodsInfo> querySkillGoods() {

//        ValueOperations valueOperations = stringRedisTemplate.opsForValue();
        //1.先那个redis中取 秒杀列表
        List<SkillGoodsInfo> skillGoodsList = (List<SkillGoodsInfo>)valueOperations.get(RedisKeyConstant.SKILL_GOODS_LIST);

        if(CollectionUtils.isEmpty(skillGoodsList)){
            skillGoodsList = goodsMapper.querySkillGoods();
            //把秒杀的商品放到redis中
            valueOperations.set(RedisKeyConstant.SKILL_GOODS_LIST,skillGoodsList,2, TimeUnit.HOURS);

//            listOperations.leftPushAll(RedisKeyConstant.SKILL_GOODS_LIST,skillGoodsList);
//
//            redisTemplate.expire(RedisKeyConstant.SKILL_GOODS_LIST,2, TimeUnit.HOURS);

        }

        return skillGoodsList;
    }

    @Override
    public GoodsDeatailData queryGoodsDeatailData(String userId, String goodsId) {
//        ValueOperations valueOperations = stringRedisTemplate.opsForValue();


        GoodsDeatailData deatailData = new GoodsDeatailData();

        //1.先从redis中获取
        GoodsDeatailData goodsDeatailData =  (GoodsDeatailData) valueOperations.get(RedisKeyConstant.SKILL_GOODS_DETAIL +goodsId);
        if(!ObjectUtils.isEmpty(goodsDeatailData)){
            //TODO 库存


            return goodsDeatailData;
        }

        GoodsDeatailInfo goodsDeatailInfo = goodsMapper.queryDeatailDataById(goodsId);
        if(ObjectUtils.isEmpty(goodsDeatailInfo)){
            return deatailData;
        }

        //bean复制，两个bean里面的字段要一样
        BeanUtils.copyProperties(goodsDeatailInfo,deatailData);

        Date startDate = goodsDeatailInfo.getStartDate();
        Date endDate =  goodsDeatailInfo.getEndDate();
        Date date = new Date();

        if (date.after(startDate) && date.before(endDate)){
            deatailData.setRemainSeconds(0);
        }

        if (date.before(startDate)){
            deatailData.setRemainSeconds(1);
        }
        //把数据库中的商品放入到redis中
        valueOperations.set(RedisKeyConstant.SKILL_GOODS_DETAIL +goodsId,deatailData,2,TimeUnit.HOURS);


        return deatailData;
    }


    public static void main(String[] args) {

//        Date startDate = goodsDeatailInfo.getStartDate();
//        Date endDate =  goodsDeatailInfo.getEndDate();

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date yes = cal.getTime();

        System.out.println("yes = " + yes);

        cal.add(Calendar.DATE,+2);

        Date tom = cal.getTime();
        System.out.println("tom = " + tom);

        Date date = new Date();


        System.err.println(date.after(date));

        System.err.println(date.before(date));



    }

    /**
     * 当项目启动的时候执行这个方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
//        ValueOperations valueOperations = stringRedisTemplate.opsForValue();

        logger.info("---------当项目启动的时候执行这个方法-------");
        //把秒杀活动的库存 初始化到mysql

        List<SkillGoods>  skillGoodsList =  skillGoodsMapper.querySkillGoods();
        if (CollectionUtils.isEmpty(skillGoodsList)){
            logger.info("没有找到可用的商品");
            return;
        }

        for (SkillGoods goods:skillGoodsList){

            String redisKey = RedisKeyConstant.SKILL_GOODS_STOCK + goods.getGoodsId();

            System.err.println("-=goods.getStockCount()===========-----" + goods.getStockCount());

            valueOperations.set(redisKey,goods.getStockCount(),2,TimeUnit.HOURS);

           System.err.println("--666666666666666666666666---"+ valueOperations.get(redisKey));

        }




    }
}
